package com.css.fxfzxczh.modules.report.repository.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.report.entity.Report;
import com.css.fxfzxczh.modules.report.repository.ReportRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ReportRepositoryImpl implements ReportRepository {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void save(Report report) {
        String sql = "insert into xczh_report(id,number,name,file_id,create_user,create_time,create_user_id,del_flag,xsem_id,rt_id,unit) " +
                "values(:id, :number, :name, :fileId, :createUser, :createTime, :createUserId, :delFlag, :xsemId, :rtId,:unit)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(report);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public List<Report> getReports(String xsemId) {
        String sql = " select * from xczh_report where xsem_id = '" + xsemId + "' and del_flag = '" + YNEnum.N.toString() + "'";
        List<Report> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Report.class));
        return list;
    }

    @Override
    public void updateReport(Report report) {
        String sql = "update xczh_report set file_id = '" + report.getFileId() + "', update_user= '" + report.getUpdateUser() + "' ,update_time = '" + report.getUpdateTime() + "' where id ='" + report.getId() + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void deleteByXsemId(String xsemId) {
        String sql = "update xczh_report set del_flag = '" + YNEnum.Y.toString() + "' where xsem_id ='" + xsemId + "'";
        jdbcTemplate.update(sql);
    }
}
